<?php
Class Job extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->data['controller'] = 'job';
        $this->load->model('Mjob');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 19, $this->session->userdata('admin_id'));
    }
    
    public function index()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['data'] = $this->Mjob->getListJobs();
        
        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addjob()
    {
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Tên nghề nghiệp', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $name = $this->getParamString('name');
            $note = $this->getParamString('note');
            $this->data['return'] = $this->Mjob->addjobDb($name,$note);
            if($this->data['return'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/job/add-job";</script>');
            }
        }
        
        $this->data['action'] = 'addjob';
        $this->load->view('template/layout', $this->data);
    }
    
    public function editjob()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Tên nghề nghiệp', 'required');
        
        $this->data['id'] = $this->security->xss_clean($this->getParamUri(4,1));
        $this->data['data'] = $this->Mjob->getjobDb($this->data['id']);        
        if($this->form_validation->run() === TRUE)
        {
            $name = $this->getParamString('name');
            $note = $this->getParamString('note');
            $this->data['return'] = $this->Mjob->editjobDb($name,$note,$this->data['id']);
            if($this->data['return'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/job/edit-job/'.$this->data['id'].'";</script>');
            }
        }
        
        $this->data['action'] = 'editjob';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deletejob()
    {
        if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['id'] = $this->security->xss_clean($this->getParamUri(4,1));
        die(json_encode($this->Mjob->deletejobDb($this->data['id'])));
    }
}